import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import lhy.autoparts.pojo.StockImport;
import lhy.orm.dao.DatabaseConnector;
import lhy.orm.dao.GenericDaoImpl;
import lhy.utils.IdUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.Executors;

public class InsertData {
    SplittableRandom random = new SplittableRandom();
    final String phones="131,132,133,134,135,136,137,138,139,151,152,153,154,155,156,157,158,170,171,172,173,174,175,176,180,181,182,183,184,185,186";
    final String[] city = {"安康市","安庆市","安顺市","安阳市","巴彦淖尔市","巴中市","白城市","白山市","白银市","百色市","蚌埠市","包头市","宝鸡市","保定市","保山市","北海市","本溪市","滨州市","沧州市","昌都地区","长春市","长沙市","长治市","常德市","常州市","巢湖市","朝阳市","潮州市","郴州市","成都市","承德市","池州市","赤峰市","崇左市","滁州市","达州市","大连市","大庆市","大同市","丹东市","德阳市","德州市","定西市","东莞市","东营市","鄂尔多斯市","鄂州市","防城港市","佛山市","福州市", "抚州市","阜新市","阜阳市","甘南州","赣州市","固原市","广安市","广元市","广州市","贵港市","贵阳市","桂林市","哈尔滨市","哈密地区","海北藏族自治州","海东地区","海口市","邯郸市","汉中市","杭州市","毫州市","合肥市","河池市","河源市","菏泽市","贺州市","鹤壁市","鹤岗市","黑河市","衡水市","衡阳市","呼和浩特市","呼伦贝尔市","湖州市","葫芦岛市","怀化市","淮安市","淮北市","淮南市","黄冈市","黄山市","黄石市","惠州市","鸡西市","吉安市","吉林市","济南市","济宁市","佳木斯市","嘉兴市","嘉峪关市","江门市","焦作市","揭阳市","金昌市","金华市","锦州市","晋城市","晋中市","荆门市","荆州市","景德镇市","九江市","酒泉市","开封市","克拉玛依市","昆明市","拉萨市","来宾市","莱芜市","兰州市","廊坊市","乐山市","丽江市","丽水市","连云港市","辽阳市","辽源市","聊城市","临沧市","临汾市","临沂市","柳州市","六安市","六盘水市","龙岩市","陇南市","娄底市","泸州市","吕梁市","洛阳市","漯河市","马鞍山市","茂名市","眉山市","梅州市","绵阳市","牡丹江市","内江市","南昌市","南充市","南京市","南宁市","南平市","南通市","南阳市","宁波市","宁德市","攀枝花市","盘锦市","平顶山市","平凉市","萍乡市","莆田市","濮阳市","普洱市","七台河市","齐齐哈尔市","钦州市","秦皇岛市","青岛市","清远市","庆阳市","曲靖市","衢州市","泉州市","日照市","三门峡市","三明市","三亚市","汕头市","汕尾市","商洛市","商丘市","上饶市","韶关市","邵阳市","绍兴市","深圳市","十堰市","石家庄市","石嘴山市","双鸭山市","朔州市","四平市","松原市","苏州市","宿迁市","宿州市","绥化市","随州市","遂宁市","台州市","太原市","泰安市","泰州市","唐山市","天水市","铁岭市","通化市","通辽市","铜川市","铜陵市","铜仁市","吐鲁番地区","威海市","潍坊市","渭南市","温州市","乌海市","乌兰察布市","乌鲁木齐市","无锡市","吴忠市","芜湖市","梧州市","武汉市","武威市","西安市","西宁市","锡林郭勒盟","厦门市","咸宁市","咸阳市","湘潭市","襄樊市","孝感市","忻州市","新乡市","新余市","信阳市","兴安盟","邢台市","徐州市","许昌市","宣城市","雅安市","烟台市","延安市","盐城市","扬州市","阳江市","阳泉市","伊春市","伊犁哈萨克自治州","宜宾市","宜昌市","宜春市","益阳市","银川市","鹰潭市","营口市","永州市","榆林市","玉林市","玉溪市","岳阳市","云浮市","运城市","枣庄市","湛江市","张家界市","张家口市","张掖市","漳州市","昭通市","肇庆市","镇江市","郑州市","中山市","中卫市","舟山市","周口市","株洲市","珠海市","驻马店市","资阳市","淄博市","自贡市","遵义市",};
    final String[] area = {"伊春区","带岭区","南岔区","金山屯区","西林区","美溪区","乌马河区","翠峦区","友好区","新青区","上甘岭区","五营区","红星区","汤旺河区","乌伊岭区","榆次区"};
    final String[] road = {"爱国路","安边路","安波路","安德路","安汾路","安福路","安国路","安化路","安澜路","安龙路","安仁路","安顺路","安亭路","安图路","安业路","安义路","安远路","鞍山路","鞍山支路","澳门路","八一路","巴林路","白城路","白城南路","白渡路","白渡桥","白兰路","白水路","白玉路","百安路（方泰镇）","百官街","百花街","百色路","板泉路","半淞园路","包头路","包头南路","宝安公路","宝安路","宝昌路","宝联路","宝林路","宝祁路","宝山路","宝通路","宝杨路","宝源路","保德路","保定路","保屯路","保屯路","北艾路",};
    final String[] home = {"家园","花园","苑","大厦","坊","佳苑","公寓","新苑","街坊","名园","花园"};
    final String[] province = {"河北省","山西省","辽宁省","吉林省","黑龙江省","江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省","广东省","海南省","四川省","贵州省","云南省","陕西省","甘肃省","青海省","台湾省",};
    final String[] Surname = {"赵","钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
            "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎",
            "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷",
            "罗", "毕", "郝", "邬", "安", "常", "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和",
            "穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒",
            "屈", "项", "祝", "董", "梁", "杜", "阮", "蓝", "闵", "席", "季"};
    final String girl = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 ";
    final String boy = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";
    final String unit="公斤,斤,磅,吨,份,刻,元,角,个,位,件,本,间,把,条,根,棵,颗,张,匹,块,片,只,架,枝,辆,幅,座,双,对,副,堆,批,群,帮,套,串,打,袋,面,粒";
    //0~9 基准200000
    final String carBrands="劳斯莱斯,宾利,捷豹,菲亚特,兰博基尼,阿尔法,布加迪,法拉利,玛莎拉蒂,保时捷," +
            //10~15 基准100000
            "奔驰,奥迪,宝马,沃尔沃,雷克萨斯,英菲尼迪," +
            //16~29 基准10000
            "凯迪拉克,别克,福特,雪佛兰,起亚,现代,日产,丰田,三菱,马自达,本田,铃木,五十铃,斯巴鲁," +
            //30~46 基准1000
            "大众,奇瑞,吉利,长城,荣威,比亚迪,昌河,长安,一汽,海马,名爵,东南汽车,五菱,江淮,东风,上汽,北汽";
    public static void main(String[] args) throws SQLException {
        InsertData ins = new InsertData();
        /*final Map<Integer,String> itemCateMap = new Object2ObjectArrayMap<>();
        ResultSet rs = DatabaseConnector.preparedStatement("select * from ItemCategory where pid is not null").executeQuery();
        while (rs.next()) {
            itemCateMap.put(rs.getInt("id"),rs.getString("name"));
        }*/

        /*

        final List<Integer> supplierIdList = new ObjectArrayList<>();
        ResultSet rs = DatabaseConnector.preparedStatement("select id from Supplier").executeQuery();
        while (rs.next()) {
            supplierIdList.add(rs.getInt("id"));
        }

        */


        //ins.insertUnit();
        //BS20220101xxxxxx
        final List<String> stockImportIdList = new ObjectArrayList<>();
        final List<String> itemInfoIdList = new ObjectArrayList<>();
        ResultSet rs = DatabaseConnector.preparedStatement("select id from StockImport").executeQuery();
        while (rs.next()) {
            stockImportIdList.add(rs.getString("id"));
        }
        rs = DatabaseConnector.preparedStatement("select id from ItemInfo").executeQuery() ;
        while (rs.next()) {
            itemInfoIdList.add(rs.getString("id"));
        }
        for (LocalDate date = LocalDate.of(2021,9,1);
             date.isBefore(LocalDate.of(2022,4,20));
             date = date.plusDays(1)) {

            LocalDate finalDate = date;
            Executors.newCachedThreadPool().execute(()->{
                try {
                    ins.insertStockImportItem(stockImportIdList,itemInfoIdList);
                    //ins.insertStockImport(finalDate,supplierIdList);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
               /* try {
                    ins.insertItemInfo(finalDate,itemCateMap);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }*/
                //insertReport1(finalDate); insertCustomerSupplier();

            });
        }

    }
    //---------------
    private void insertReport1(LocalDate finalDate){
        SplittableRandom rand = new SplittableRandom();
        int nextInt = rand.nextInt(100000, 999999);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {
            DatabaseConnector.preparedStatement(String.format("insert into LossReport values ('%s','%s','%s','%s')",
                    "BS"+ finalDate.format(formatter)+nextInt, finalDate.format(formatter2),"admin","")).executeUpdate();
            DatabaseConnector.preparedStatement(String.format("insert into OverflowReport values ('%s','%s','%s','%s')",
                    "BY"+ finalDate.format(formatter)+nextInt, finalDate.format(formatter2),"admin","")).executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    private void insertCustomerSupplier(){
        String city = getCity();
        try {
            DatabaseConnector.preparedStatement(String.format("insert into Supplier (name,contact,phone,address) values ('%s','%s','%s','%s')",
                    city.replace("市","")+getCorpName()+ "科技有限公司",  getName(),getPhones(),city+getAddress())).executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //插入单位，自带循环
    private void insertUnit(){
            String[] units = unit.split(",");
                for(String unit:units){
                    Executors.newCachedThreadPool().execute(()->{
                        try {
                            DatabaseConnector.preparedStatement(String.format("insert into ItemUnit (name) values ('%s')", unit)).executeUpdate();
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    });

                }
    }
    private void insertItemInfo(LocalDate date, Map<Integer,String> itemCategoryMap) throws SQLException {
        String[] splitCarBrand = carBrands.split(",");
        String id = "SP"+IdUtils.generateIdWithDate(date);
        List<Map.Entry<Integer, String>> entrySet = new ArrayList<>(itemCategoryMap.entrySet().stream().toList());
        Collections.shuffle(entrySet);
        Map.Entry<Integer, String> cateRand = entrySet.get(random.nextInt(entrySet.size()-1));
        int cate_id = cateRand.getKey();
        String cate_name = cateRand.getValue();
        final int unit_id=7;
        int supplier_id = random.nextInt(6,246);
        int randomCarBrand = random.nextInt(0,splitCarBrand.length-1);
        double sellPrice = 0;
        if(randomCarBrand<=9)
            sellPrice = random.nextDouble(200000,300000);
        if(randomCarBrand<=15)
            sellPrice = random.nextDouble(50000,100000);
        if(randomCarBrand<=29)
            sellPrice = random.nextDouble(5000,20000);
        if(randomCarBrand<=46)
            sellPrice = random.nextDouble(1000,10000);
        double avgCost = sellPrice * 0.8;
        String itemName = splitCarBrand[randomCarBrand] + IdUtils.generateModel() + cate_name;
        DatabaseConnector.preparedStatement(String.format("insert into ItemInfo values ('%s','%s','%s','%s','%s','%s','%s','%s','%s')",
                id,
                itemName,
                IdUtils.generateModel()  ,
                cate_id,
                unit_id,
                Math.round(avgCost*100.0)/100.0,
                Math.round(sellPrice*100.0)/100.0,
                supplier_id,
                "")).executeUpdate();
    }
    private void insertStockImport(LocalDate date,List<Integer> supplierIdList) throws SQLException {
        String id = "JH"+IdUtils.generateIdWithDate(date);
        int suppplierId = supplierIdList.get(random.nextInt(supplierIdList.size()-1));
        String dates = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        double accPay = random.nextDouble(1000,1000000);
        double actRec = accPay;
        DatabaseConnector.preparedStatement(String.format("insert into StockImport values ('%s','%s','%s','%s','%s','%s','%s')",id,suppplierId,date,accPay,actRec,"admin","")).executeUpdate();
    }
    private void insertStockImportItem(List<String> stockIdList,List<String> itemInfoIdList) throws SQLException {
        for (int i = 0; i < 5; i++) {
            String stockId = stockIdList.get(random.nextInt(stockIdList.size()-1));
            String itemInfoId = itemInfoIdList.get(random.nextInt(stockIdList.size()-1));
            double singlePrice = random.nextDouble(10,10000);
            int amount = random.nextInt(10,100);
            DatabaseConnector.preparedStatement(String.format("insert into StockImportItem (stockImport_id,item_id,singlePrice,amount,comment) values ('%s','%s','%s','%s','%s')",stockId,itemInfoId,singlePrice,amount,"")).executeUpdate();

        }


    }
    //---------------
    public String getCity(){
        int randomCityNum = random.nextInt(city.length);
        return city[randomCityNum];
    }
    public String getCorpName(){
        String name = getName().substring(1);
        if(name.length()==1)
            name += getName().substring(1);

        return name;
    }
    public String getAddress(){

        //

        int randomAreaNum = random.nextInt(area.length);
        int randomRoadNum = random.nextInt(road.length);
        int randomHomeNum = random.nextInt(home.length);
        int num = random.nextInt(1000);
        return area[randomAreaNum]+road[randomRoadNum]+num+"号"+getCorpName()+home[randomHomeNum];

    }


    public String getPhones(){
        String[] split = phones.split(",");
        int len = split.length-1;
        String phoneHead = split[random.nextInt(len)];
        int number = random.nextInt(10000000,99999999);
        return phoneHead+number;
    }
    public String getName() {
       int index = random.nextInt(Surname.length - 1);
        String name = Surname[index]; //获得一个随机的姓氏
        int i = random.nextInt(3);//可以根据这个数设置产生的男女比例
        if(i==2){
            int j = random.nextInt(girl.length()-2);
            if (j % 2 == 0) {
                name =  name + girl.substring(j, j + 2);
            } else {
                name =  name + girl.substring(j, j + 1);
            }

        }
        else{
            int j = random.nextInt(girl.length()-2);
            if (j % 2 == 0) {
                name =  name + boy.substring(j, j + 2);
            } else {
                name =   name + boy.substring(j, j + 1);
            }

        }

        return name;
    }

}
